package com.sita.linboard.person;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.base.ActivityCollector;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.driverInfo.ClearBackBean;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.http.Part;

/**
 * Created by Jason on 2016/9/26.
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener{
    private LinearLayout back;
    private TextView head,submit;
    private EditText messageTx,address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void initData() {
        back.setOnClickListener(this);
        back.setVisibility(View.VISIBLE);
        head.setText("意见反馈");
        head.setVisibility(View.VISIBLE);
        submit.setOnClickListener(this);
        submit.setText("提交");
        submit.setVisibility(View.VISIBLE);
        submit.setTextColor(ContextCompat.getColor(BaseApplication.getContext(),R.color.main_text_color));

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
         back = bindView(R.id.img_back);
        head = bindView(R.id.head_title);
        submit = bindView(R.id.clear_all_text);
        messageTx = bindView(R.id.Et_Msg);
        address = bindView(R.id.Et_Address);
    }

    @Override
    protected int getId() {

        return R.layout.activity_feedback;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.clear_all_text:
                getData();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActivityCollector.removeActivity(this);
    }

    public static void FeedBackIntent(Context context){
        Intent intent = new Intent(context,FeedBackActivity.class);
        context.startActivity(intent);
    }
    private void getData(){
        FeedBackRequest request = new FeedBackRequest();
        request.userId =  SpUtils.getString("AccoundID",null,BaseApplication.getContext());
        if (messageTx.getText().length()==0){
            ToastUtils.show("请填写反馈消息",3);
            return;
        }
        else {
            request.content = messageTx.getText().toString();
            if (address.getText().length()==0){
                ToastUtils.show("请填写联系方式",3);
                return;
            }
            else {
                request.contactWay = address.getText().toString();
                RestClient.getRestService().feedBack(request, new Callback<ClearBackBean>() {
                    @Override
                    public void success(ClearBackBean clearBackBean, Response response) {
                        if (clearBackBean.getErrorCode().equals("0")){
                            ToastUtils.show("感谢您的反馈",3);
                        }
                        else {
                            ToastUtils.show("反馈信息上传失败",3);
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {
                        ToastUtils.show("反馈信息上传失败",3);
                    }
                });
            }
        }

    }
}
